package modbus

import "fmt"

type SpindleStatus struct {
	Brokens      []uint16
	Weaks        []uint16
	Noocupations []uint16
	Malfs        []uint16
	Bypass       []uint16
}

func (s *SpindleStatus) Init() {
	s.Brokens = []uint16{}
	s.Weaks = []uint16{}
	s.Noocupations = []uint16{}
	s.Malfs = []uint16{}
	s.Bypass = []uint16{}
}

// 将所有异常锭子数据封装成 如下 json形式： {"1":[1,2,4], "2":[5]}
func (s *SpindleStatus) ToJsonString() string {

	str := ""
	if len(s.Brokens) > 0 {
		str += fmt.Sprintf("\"1\":%v", s.Brokens)
	}
	if len(s.Weaks) > 0 {
		if len(str) > 0 {
			str += "," + fmt.Sprintf("\"2\":%v", s.Weaks)
		} else {
			str += fmt.Sprintf("\"2\":%v", s.Weaks)
		}
	}
	if len(s.Noocupations) > 0 {
		if len(str) > 0 {
			str += "," + fmt.Sprintf("\"3\":%v", s.Noocupations)
		} else {
			str += fmt.Sprintf("\"3\":%v", s.Noocupations)
		}
	}
	if len(s.Malfs) > 0 {
		if len(str) > 0 {
			str += "," + fmt.Sprintf("\"4\":%v", s.Malfs)
		} else {
			str += fmt.Sprintf("\"4\":%v", s.Malfs)
		}
	}
	if len(s.Bypass) > 0 {
		if len(str) > 0 {
			str += "," + fmt.Sprintf("\"5\":%v", s.Bypass)
		} else {
			str += fmt.Sprintf("\"5\":%v", s.Bypass)
		}
	}

	return fmt.Sprintf("{%v}", str)
}

func (s *SpindleStatus) Add(spin_no uint16, status uint16) {

	switch status {
	case 1:
		s.Brokens = append(s.Brokens, spin_no)
	case 2:
		s.Weaks = append(s.Weaks, spin_no)
	case 3:
		s.Noocupations = append(s.Noocupations, spin_no)
	case 4:
		s.Malfs = append(s.Malfs, spin_no)
	case 5:
		s.Bypass = append(s.Bypass, spin_no)
	}
}
